/**
 * Created by zhaojiuyang1 on 15/7/7.
 */
var gpio = require("pi-gpio");
var Q = require("q");
var moment = require("moment");
var _ = require("underscore");
var emitter = require('./emitter');
var inputGPIO = [11];
var outputGPIO = [7];
var openInput = function(gnum){
    var deferred = Q.defer();
    gpio.open(gnum,"input",function(err){
        if(!_.isNull(err)){
            return deferred.reject(err);
        }else{
            console.log("open %s is input",gnum);
            return deferred.resolve();
        }
    });
    return deferred.promise;
};
var openOutput = function(gnum){
    var deferred = Q.defer();
    gpio.open(gnum,"output",function(err){
        if(!_.isNull(err)){
            return deferred.reject(err);
        }else{
            console.log("open %s is output",gnum);
            return deferred.resolve();
        }
    });
    return deferred.promise;
};
var openAll = function(){
    var deferred = Q.defer();
    var promises = [];
    promises = promises.concat(_.map(inputGPIO,function(gnum){
        return openInput(gnum);
    }),_.map(outputGPIO,function(gnum){
        return openOutput(gnum);
    }));
    Q.all(promises)
        .then(function(){
            deferred.resolve();
        }).fail(function(err){
            console.error(err);
            deferred.reject(err);
        });
    return deferred.promise;
};

var output7 = function(value){
    var deferred = Q.defer();
    gpio.write(7,value,function(err){
        if(!_.isNull(err)) return deferred.reject(err);
        deferred.resolve();
    });
    return deferred.promise;
};
function autoSend(){
    output7(1)
        .then(function(){
            console.log("send out 1 on 7");
            setTimeout(function(){
                console.log("send out 0 on 7");
                output7(0).then(function(){
                    emitter.emit("hasSend");
                    setTimeout(function(){
                        autoSend();
                    },3000);
                })
            },0.01);
        });
}

var getValue11 = function(){
    var deferred = Q.defer();
    gpio.read(11,function(err,value){
        if(!_.isNull(err)){
            return deferred.reject(err);
        }
        deferred.resolve(value);
    });
    return deferred.promise;
};
emitter.addListener("hasSend",function(){
    var timeStart = 0;
    var timeEnd = 0;
    function getValue(){
        getValue11.then(function(value){
            if(value == 1 && timeStart === 0){
                timeStart = new Date().getTime();
            }
            if(value === 0 && timeEnd === 0 && timeStart!=0){
                timeEnd = new Date().getTime();
            }
            if(timeStart != 0 && timeEnd != 0){
                return timeEnd - timeStart;
            }else{
                getValue();
            }
        })
    }
    var len = getValue();
    var pathLen = 340 * (len / 1000);
    console.log("distance is : %s",pathLen);
});
emitter.on("error",function(){
    gpio.close(7);
    gpio.close(11);
});

openAll()
    .then(function(){
        autoSend();
    }).fail(function(err){
        console.log(err);
        gpio.close(7);
        gpio.close(11);
    });
